package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.SetmealDao;
import cn.usian.dao.SetmealDishDao;
import cn.usian.pojo.Setmeal;
import cn.usian.pojo.SetmealDish;
import cn.usian.pojo.SetmealDto;
import cn.usian.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealDao setmealDao;
    @Resource
    private SetmealDishDao setmealDishDao;

    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDao> setmealPage = new Page<>(page, pageSize);
        List<SetmealDao> selectPage = setmealDao.findPage(setmealPage, name);
        setmealPage.setRecords(selectPage);
        return R.success(setmealPage);
    }

    @Override
    public R save(SetmealDto setmealDTO) {
        setmealDTO.setStatus(1);
        setmealDTO.setCreateTime(LocalDateTime.now());
        setmealDTO.setUpdateTime(LocalDateTime.now());

        setmealDao.insert(setmealDTO);

        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDish.setCreateUser(setmealDTO.getCreateUser());
            setmealDish.setUpdateUser(setmealDTO.getUpdateUser());
            setmealDish.setIsDeleted(0);
            setmealDishDao.insert(setmealDish);
        }

        return R.success("添加成功");
    }

    @Override
    public R findById(Long id) {
        SetmealDto setmealDTO = new SetmealDto();
        Setmeal setmeal = setmealDao.selectById(id);

        List<SetmealDish> setmealDishList=setmealDishDao.findList(id);
        BeanUtils.copyProperties(setmeal,setmealDTO);

        setmealDTO.setSetmealDishes(setmealDishList);

        return R.success(setmealDTO);
    }

    @Override
    public R edit(SetmealDto setmealDTO) {
        setmealDTO.setUpdateTime(LocalDateTime.now());
        setmealDTO.setStatus(1);
        setmealDao.updateById(setmealDTO);
        //删除原有的
        setmealDishDao.delDishSetmeal(setmealDTO.getId());

        //添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDish.setCreateUser(setmealDTO.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealDTO.getCreateUser());
            setmealDish.setUpdateUser(setmealDTO.getUpdateUser());
            setmealDish.setIsDeleted(0);
            setmealDishDao.insert(setmealDish);
        }
        return R.success("修改成功");
    }
}
